package com.woniuxy.yoga.business.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.business.model.dto.storeclassroom.ClassroomNormalDto;
import com.woniuxy.yoga.business.model.dto.storeclassroom.StoreClassroomAddDTO;
import com.woniuxy.yoga.business.model.dto.storeclassroom.StoreClassroomByIdDTO;
import com.woniuxy.yoga.business.model.dto.storeclassroom.StoreClassroomUpdateDTO;
import com.woniuxy.yoga.business.model.form.storeclassroom.*;
import com.woniuxy.yoga.business.model.param.storeclassroom.*;
import com.woniuxy.yoga.business.service.StoreClassroomService;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店教室表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/storeClassroom")
@Api(tags = "门店教室相关接口")
@Validated
public class StoreClassroomController {


    @Autowired
    private StoreClassroomService storeClassroomService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 门店添加教室
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/addStoreClassroom.bu")
    @ApiOperation("添加门店教室")
    @YogaLog("添加门店教室")
    public Result<StoreClassroomAddDTO> addStoreClassroom(@Valid @RequestBody StoreClassroomAddForm form, @RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        StoreClassroomAddParam param = BeanUtil.toBean(form, StoreClassroomAddParam.class);
        param.setAddUserId(businessId);
        StoreClassroomAddDTO dto = storeClassroomService.addStoreClassroom(param);
        return Result.getResult(StoreClassroomAddDTO.class).setMessage("添加成功").setData(dto);
    }

    /**
     * 修改教室名字
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/updateStoreClassroom.bu")
    @ApiOperation("修改教室信息")
    @YogaLog("修改教室信息")
    public Result<StoreClassroomUpdateDTO> updateStoreClassroom(@Valid@RequestBody StoreClassroomUpdateForm form, @RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        StoreClassroomUpdateParam param = BeanUtil.toBean(form, StoreClassroomUpdateParam.class);
        param.setUpdateUserId(businessId);
        StoreClassroomUpdateDTO dto = storeClassroomService.updateStoreClassroom(param);
        return Result.getResult(StoreClassroomUpdateDTO.class).setMessage("修改成功").setData(dto);
    }

    /**
     * 商家删除教室
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/deleteStoreClassroom.bu")
    @ApiOperation("商家删除教室")
    @YogaLog("商家删除教室")
    public Result<StoreClassroomUpdateDTO> deleteStoreClassroom(@Valid@RequestBody StoreClassroomDeleteForm form, @RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        StoreClassroomDeleteParam param = BeanUtil.toBean(form, StoreClassroomDeleteParam.class);
        param.setAccountId(businessId);
        storeClassroomService.deleteStoreClassroom(param);
        return Result.getResult(StoreClassroomUpdateDTO.class).setMessage("删除成功");
    }

    /**
     * 分页查询场馆教室信息
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/selectPageStoreClassroomInfo.bu")
    @ApiOperation("分页查询场馆教室信息")
    public Result<PageInfo> selectPageStoreClassroomInfo(@RequestBody StoreSelectClassroomPageForm form,@RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        StoreSelectClassroomPageParam param = BeanUtil.toBean(form, StoreSelectClassroomPageParam.class);
        param.setAccountId(businessId);
        PageInfo pageInfo = storeClassroomService.selectPageStoreClassroomInfo(param);
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }

    /**
     * 查询所有的教室，并按 id->详情 存储
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/selNormalByIds")
    @ApiOperation("查询所有的教室，并按 id->详情 存储")
    public Result<Map> selNormalByIds(@Valid@RequestBody StoreClassroomIdsForm form)throws Exception{
        Map<Integer, ClassroomNormalDto> dtos = storeClassroomService.selNormalByIds(BeanUtil.toBean(form, StoreClassroomIdsParam.class));
        return Result.getResult(Map.class).setData(dtos);
    }

    /**
     * 根据教室id查询教室信息
     * @param storeClassroomId
     * @return
     * @throws Exception
     */
    @GetMapping("/selNormalById/{storeClassroomId}")
    @ApiOperation("根据教室id查询教室信息")
    public Result<StoreClassroomByIdDTO> selNormalById(@PathVariable("storeClassroomId") Integer storeClassroomId)throws Exception{
        StoreClassroomByIdDTO dto = storeClassroomService.selNormalById(storeClassroomId);
        return Result.getResult(StoreClassroomByIdDTO.class).setData(dto);
    }

    /**
     * 商家查询场馆所有可用教室
     * @param authen_token
     * @return
     */
    @PostMapping("/getAllNormalClassroomsByBusinessId.bu")
    @ApiOperation("商家查询场馆所有可用教室")
    public Result<List<ClassroomNormalDto>> getAllNormalClassroomsByMerchantId(@RequestHeader String authen_token) {
        List<ClassroomNormalDto> roomNormalDtos = storeClassroomService.getAllNormalClassroomsByBusinessId(JWTUtil.getBusinessId(authen_token));
        return Result.getListResult(ClassroomNormalDto.class).setData(roomNormalDtos).setMessage("查询成功");
    }

    /**
     * 禁用教室
     * @param classroomId 教室id
     * @return none
     */
    @GetMapping("/disableClassroom/{classroomId}.bu")
    @ApiOperation("禁用教室")
    @YogaLog("禁用教室")
    public Result<?> disableClassroom(@PathVariable @Valid @NotNull Integer classroomId){
        storeClassroomService.disableClassroom(classroomId);
        return Result.getResult().setMessage("禁用教室成功");
    }

    /**
     * 启用教室
     * @param classroomId 教室id
     * @return none
     */
    @GetMapping("/enableClassroom/{classroomId}.bu")
    @ApiOperation("启用教室")
    @YogaLog("启用教室")
    public Result<?> enableClassroom(@PathVariable @Valid @NotNull Integer classroomId){
        storeClassroomService.enableClassroom(classroomId);
        return Result.getResult().setMessage("启用教室成功");
    }

    /**
     * 商家查询可用教室集合
     * @param storeId storeId
     * @return List<ClassroomNormalDto>
     */
    @GetMapping("/findUsefulClassroom/{storeId}.bu")
    @ApiOperation("商家查询可用教室集合")
    @YogaLog("商家查询可用教室集合")
    public Result<List<ClassroomNormalDto>> findUsefulClassroom(@PathVariable @Valid @NotNull Integer storeId){
        List<ClassroomNormalDto> classroomNormalDtos =storeClassroomService.findUsefulClassroom(storeId);
        return Result.getListResult(ClassroomNormalDto.class).setMessage("查询成功").setData(classroomNormalDtos);
    }
}

